package com.louis.iread.bean;

import com.louis.iread.IReadApplication;

import org.xutils.DbManager;
import org.xutils.db.annotation.Column;
import org.xutils.db.annotation.Table;
import org.xutils.ex.DbException;

import java.io.IOException;

import taobe.tec.jcc.JChineseConvertor;

/**
 * Created by louis on 17-4-5.
 */

@Table(name = "article")
public class Article {

    @Column(name = "id", isId = true)
    private Long id;    //小说章节id

    @Column(name = "bookId")
    private Long bookId;    //小说id

    @Column(name = "title")
    private String title;   //章节标题

    @Column(name = "url", property = "UNIQUE")
    private String url; //章节url

    @Column(name = "downloadTime")
    private String downloadTime; //章节下载时间

    @Column(name = "readTime")
    private String readTime; //章节阅读时间

    @Column(name = "isDownload")
    private boolean isDownload; //章节是否已经下载

    @Column(name = "isRead")
    private boolean isRead; //章节是否已经阅读

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getBookId() {
        return bookId;
    }

    public void setBookId(Long bookId) {
        this.bookId = bookId;
    }

    public String getTitle() {
        if (IReadApplication.bIsTraditional) {
            try {
                return JChineseConvertor.getInstance().s2t(title);
            } catch (IOException e) {
                e.printStackTrace();
                return title;
            }
        }
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getDownloadTime() {
        return downloadTime;
    }

    public void setDownloadTime(String downloadTime) {
        this.downloadTime = downloadTime;
    }

    public String getReadTime() {
        return readTime;
    }

    public void setReadTime(String readTime) {
        this.readTime = readTime;
    }

    public boolean isDownload() {
        return isDownload;
    }

    public void setDownload(boolean download) {
        isDownload = download;
    }

    public boolean isRead() {
        return isRead;
    }

    public void setRead(boolean read) {
        isRead = read;
    }

    public Book getBook(DbManager dbManager) throws DbException {
        return dbManager.findById(Book.class, bookId);
    }

    public ArticleContent getArticleContent(DbManager dbManager) throws DbException {
        return dbManager.selector(ArticleContent.class).where("articleId", "=", this.id).findFirst();
    }
}
